package net.orange.architecture.system;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * System parameters, which include the different system configuration, like
 * server character setting / regional setting / resource file path.
 */
public class SystemParameters {

	public static Properties props = null;

	public static String SRV_DATE_FORMAT = "";

	public static String SRV_TIME_FORMAT = "";

	public static String SRV_DATETIME_FORMAT = "";

	public static String HTTP_RESOURCE_ROOT = "";

	public static String HTTP_RESOURCE_URL = "";

	public static boolean ARCH_LOG_ACTION = false;

	public static boolean ARCH_LOG_DB = false;

	public static boolean ARCH_LOG_EXCEPTION = false;

	public static boolean ARCH_LOG_JOB = false;

	public static boolean ARCH_LOG_SECRITY = false;

	public static String[] ARCH_APPCONTEXT_FILES = null;

	public static String AD_ACTION_DIR = "";

	public static String AD_DB_DIR = "";

	public static String AD_EXCEPTION_DIR = "";

	public static String AD_SECURITY_DIR = "";

	public static String AD_SC_DIR = "";

	public static boolean AD_EXCEPTION_LOG_DETAIL = false;

	public static int JDBC_BATCH_NUMNER = 5;

	public static String RESOURCE_DATA_TMP = "";

	public static String RESOURCE_DATA_CLIENT = "";

	public static String RESOURCE_DATA_PIC = "";

	static {
		Initialize();
	}

	/**
	 * initialize the properties
	 */
	private static void Initialize() {
		props = new Properties();
		load(SystemResource.ARCHITECTURE_CONFIG_FILE);
		load(SystemResource.RESOURCE_CONFIG_FILE);
		load(SystemResource.AUDITOR_CONFIG_FILE);
		loadParameters();
	}

	/**
	 * Load the parameters.
	 */
	private static void loadParameters() {
		SRV_DATE_FORMAT = get("SRV_DATE_FORMAT");
		SRV_TIME_FORMAT = get("SRV_TIME_FORMAT");
		SRV_DATETIME_FORMAT = get("SRV_DATETIME_FORMAT");

		HTTP_RESOURCE_ROOT = get("HTTP_RESOURCE_ROOT");
		HTTP_RESOURCE_URL = get("HTTP_RESOURCE_URL");

		ARCH_LOG_ACTION = Boolean.valueOf(get("ARCH_LOG_ACTION"));
		ARCH_LOG_DB = Boolean.valueOf(get("ARCH_LOG_DB"));
		ARCH_LOG_EXCEPTION = Boolean.valueOf(get("ARCH_LOG_EXCEPTION"));
		ARCH_LOG_JOB = Boolean.valueOf(get("ARCH_LOG_JOB"));
		ARCH_LOG_SECRITY = Boolean.valueOf(get("ARCH_LOG_SECRITY"));

		ARCH_APPCONTEXT_FILES = get("ARCH_APPCONTEXT_FILES").split(",");

		AD_ACTION_DIR = get("AD_ACTION_DIR");
		AD_DB_DIR = get("AD_DB_DIR");
		AD_EXCEPTION_DIR = get("AD_EXCEPTION_DIR");
		AD_SECURITY_DIR = get("AD_SECURITY_DIR");
		AD_SC_DIR = get("AD_SC_DIR");

		AD_EXCEPTION_LOG_DETAIL = Boolean.valueOf(
				get("AD_EXCEPTION_LOG_DETAIL")).booleanValue();

		RESOURCE_DATA_TMP = get("RESOURCE_DATA_TMP");
		RESOURCE_DATA_CLIENT = get("RESOURCE_DATA_CLIENT");
		RESOURCE_DATA_PIC = get("RESOURCE_DATA_PIC");
	}

	/**
	 * Load the parameters form the file.
	 * 
	 * @param filename
	 *            The file name
	 */
	private static void load(String filename) {
		InputStream is = null;
		Properties p = new Properties();
		try {
			p.clear();
			is = SystemParameters.class.getClassLoader().getResourceAsStream(
					filename);
			p.load(is);

			if (p != null) {
				Enumeration e = p.propertyNames();
				while (e.hasMoreElements()) {
					String key = (String) e.nextElement();
					props.put(key, p.get(key));

				}
			}
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("[Fatal Error] Can't initialize the file: "
					+ filename + ".properties");
		}
	}

	/**
	 * Get the value in properties file.
	 * 
	 * @return
	 */
	private static String get(String key) {
		return props.getProperty(key);
	}
}
